package com.wxapp.sale.service.service.impl;

import com.wxapp.common.base.util.redis.RedisUtil;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.constant.CatchCommonConstant;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.copy.CopyUtil;
import com.wxapp.sale.api.pojo.vm.shopCart.PShopCartListVm;
import com.wxapp.sale.api.pojo.vo.shopCart.QShopCartAddVo;
import com.wxapp.sale.service.service.ShopCartService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description
 *
 * @author lgl
 * DateTime  2024/2/1 9:28
 * Modified By
 */
@Service
@RequiredArgsConstructor
public class ShopCartServiceImpl extends IBaseService implements ShopCartService {
    private final RedisUtil redisUtil;

    @Override
    public ResultVm<List<PShopCartListVm>> list(UserVo userVo) {
        var objectObjectMap = redisUtil.hMGet(CatchCommonConstant.SHOP_CART + userVo.getId());
        return success(CopyUtil.copyListNew(new ArrayList<>(objectObjectMap.values()), PShopCartListVm::new, null));
    }

    @Override
    public ResultVm<String> add(UserVo userVo, QShopCartAddVo qShopCartAddVo) {
        var objectObjectMap = redisUtil.hGet(CatchCommonConstant.SHOP_CART + userVo.getId(), qShopCartAddVo.getId());
        if (CheckUtil.isNotNullObject(objectObjectMap)) {
            return error("已经在购物车中");
        }
        redisUtil.hSet(CatchCommonConstant.SHOP_CART + userVo.getId(), qShopCartAddVo.getId(), qShopCartAddVo, (365 / 2) * 24 * 3600);
        return success("加入购物车");
    }

    @Override
    public ResultVm<String> delete(UserVo userVo, List<String> id) {
        if (CheckUtil.isNotNullList(id)) {
            redisUtil.hDel(CatchCommonConstant.SHOP_CART + userVo.getId(), id);
        }
        return success("删除购物车");
    }
}
